import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from '@nestjs/swagger';
import { CarouselService } from './carousel.service';
import { Carousel } from '@libs/db/models/carousel.model';
import { Public } from 'src/decorations/public.decorator';

@ApiTags('轮播图管理')
@Controller('carousel')
export class CarouselController {
  constructor(private readonly carouselService: CarouselService) { }

  /**
   * 创建轮播图
   * @param createCarouselDto - 创建轮播图的数据（来源：请求体）
   * @returns 创建的轮播图信息
   */
  @Post()
  @ApiOperation({ summary: '创建轮播图' })
  @ApiResponse({ status: 201, description: '创建成功', type: Carousel })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 500, description: '服务器错误' })
  async create(@Body() createCarouselDto: Partial<Carousel>) {
    try {
      return await this.carouselService.create(createCarouselDto);
    } catch (error) {
      throw new HttpException(
        error.message || '创建轮播图失败',
        error.status || HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  /**
   * 分页查询轮播图列表
   * @param pageNum - 页码（来源：查询参数）
   * @param pageSize - 每页数量（来源：查询参数）
   * @param group - 分组过滤（来源：查询参数）
   * @returns 轮播图列表和分页信息
   */
  @Get()
  @ApiOperation({ summary: '分页查询轮播图列表' })
  @ApiQuery({ name: 'pageNum', required: false, description: '页码', example: 1 })
  @ApiQuery({ name: 'pageSize', required: false, description: '每页数量', example: 10 })
  @ApiQuery({ name: 'group', required: false, description: '分组过滤' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @ApiResponse({ status: 500, description: '服务器错误' })
  @Public()
  async findAll(
    @Query('pageNum') pageNum?: string,
    @Query('pageSize') pageSize?: string,
    @Query('group') group?: string,
  ) {
    try {
      const page = pageNum ? parseInt(pageNum, 10) : 1;
      const size = pageSize ? parseInt(pageSize, 10) : 10;

      return await this.carouselService.findAll(page, size, group);
    } catch (error) {
      throw new HttpException(
        error.message || '查询轮播图列表失败',
        error.status || HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  /**
   * 获取所有轮播图（不分页）
   * @param group - 分组过滤（来源：查询参数）
   * @returns 轮播图列表
   */
  @Get('all')
  @ApiOperation({ summary: '获取所有轮播图（不分页）' })
  @ApiQuery({ name: 'group', required: false, description: '分组过滤' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @ApiResponse({ status: 500, description: '服务器错误' })
  async findAllWithoutPagination(@Query('group') group?: string) {
    try {
      return await this.carouselService.findAllWithoutPagination(group);
    } catch (error) {
      throw new HttpException(
        error.message || '查询所有轮播图失败',
        error.status || HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  /**
   * 根据ID查询轮播图详情
   * @param id - 轮播图ID（来源：路径参数）
   * @returns 轮播图详情
   */
  @Get(':id')
  @ApiOperation({ summary: '根据ID查询轮播图详情' })
  @ApiParam({ name: 'id', description: '轮播图ID' })
  @ApiResponse({ status: 200, description: '查询成功', type: Carousel })
  @ApiResponse({ status: 404, description: '轮播图不存在' })
  @ApiResponse({ status: 500, description: '服务器错误' })
  async findOne(@Param('id') id: string) {
    try {
      return await this.carouselService.findOne(id);
    } catch (error) {
      throw new HttpException(
        error.message || '查询轮播图详情失败',
        error.status || HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  /**
   * 更新轮播图
   * @param id - 轮播图ID（来源：路径参数）
   * @param updateCarouselDto - 更新数据（来源：请求体）
   * @returns 更新后的轮播图信息
   */
  @Patch(':id')
  @ApiOperation({ summary: '更新轮播图' })
  @ApiParam({ name: 'id', description: '轮播图ID' })
  @ApiResponse({ status: 200, description: '更新成功', type: Carousel })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 404, description: '轮播图不存在' })
  @ApiResponse({ status: 500, description: '服务器错误' })
  async update(
    @Param('id') id: string,
    @Body() updateCarouselDto: Partial<Carousel>,
  ) {
    try {
      return await this.carouselService.update(id, updateCarouselDto);
    } catch (error) {
      throw new HttpException(
        error.message || '更新轮播图失败',
        error.status || HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  /**
   * 删除轮播图
   * @param id - 轮播图ID（来源：路径参数）
   * @returns 删除结果
   */
  @Delete(':id')
  @ApiOperation({ summary: '删除轮播图' })
  @ApiParam({ name: 'id', description: '轮播图ID' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 404, description: '轮播图不存在' })
  @ApiResponse({ status: 500, description: '服务器错误' })
  async remove(@Param('id') id: string) {
    try {
      return await this.carouselService.remove(id);
    } catch (error) {
      throw new HttpException(
        error.message || '删除轮播图失败',
        error.status || HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  /**
   * 批量删除轮播图
   * @param body - 包含ID数组的请求体（来源：请求体）
   * @returns 删除结果
   */
  @Delete('batch/delete')
  @ApiOperation({ summary: '批量删除轮播图' })
  @ApiResponse({ status: 200, description: '批量删除成功' })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 500, description: '服务器错误' })
  async removeMany(@Body() body: { ids: string[] }) {
    try {
      return await this.carouselService.removeMany(body.ids);
    } catch (error) {
      throw new HttpException(
        error.message || '批量删除轮播图失败',
        error.status || HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }
}